package cn.maihe.elg.operation.utils;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
/**
 * @Description
 * @Author WJH
 * @Date 2021/11/03
 */
public class SM4UtilsT {
    public static final String ALGORITHM_NAME = "SM4";
    public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";
    public static final int DEFAULT_KEY_SIZE = 128;

    static
    {
        try
        {
            Security.addProvider(new BouncyCastleProvider());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static byte[] encrypt_Ecb_Padding(byte[] key, byte[] data)
            throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
    {
        Cipher cipher = generateEcbCipher("SM4/ECB/PKCS7Padding", 1, key);
        return cipher.doFinal(data);
    }

    public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText)
            throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException
    {
        Cipher cipher = generateEcbCipher("SM4/ECB/PKCS5Padding", 2, key);
        return cipher.doFinal(cipherText);
    }

    private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key)
            throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException
    {
        Cipher cipher = Cipher.getInstance(algorithmName, "BC");
        Key sm4Key = new SecretKeySpec(key, "SM4");
        cipher.init(mode, sm4Key);
        return cipher;
    }

    public static byte[] generateKey()
            throws NoSuchAlgorithmException, NoSuchProviderException
    {
        return generateKey(128);
    }

    public static byte[] generateKey(int keySize)
            throws NoSuchAlgorithmException, NoSuchProviderException
    {
        KeyGenerator kg = KeyGenerator.getInstance("SM4", "BC");
        kg.init(keySize, new SecureRandom());
        return kg.generateKey().getEncoded();
    }

    public static void main(String[] args)
            throws UnsupportedEncodingException, NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException, InvalidAlgorithmParameterException {

        String gmKey = "oweKG6vCSt1PZQX1/n7OLg==";
        byte[] bKey = Base64.decodeBase64(gmKey);

        String value = "【国泰测试】gml-7月份关于解密临界点的整改-工程-1";
        byte[] eData = encrypt_Ecb_Padding(bKey, value.getBytes("UTF-8"));
        String enVal = Base64.encodeBase64String(eData);

        byte[] eData2 = SM4Util.encrypt_ECB_Padding(bKey, value.getBytes("UTF-8"));
        String enVal2 = Base64.encodeBase64String(eData2);



        System.out.println("秘钥key："+gmKey);
        System.out.println("明文内容："+value);
        System.out.println("本地加密1："+enVal);
        System.out.println("本地加密2："+enVal2);
        String cEnVal = "v+LNYbpcXHhO7hxqQSzmQ0plAcZtqNbfTNWUFrcN1x0WMhDJG3drpbxZTFeJY2WWI5TnRngzpWicDAPS1WB5HcbQ4+5cuxhKon8aYRFVktY=";
        System.out.println("三明加密："+cEnVal);

        byte[] cEnData =  Base64.decodeBase64(cEnVal);
        byte[] e1Data =  Base64.decodeBase64(enVal);
        byte[] e2Data =  Base64.decodeBase64(enVal2);

//        byte[] cByte01 = decrypt_Ecb_Padding(bKey, cEnData);
//        byte[] cByte02 = SM4Util.decrypt_ECB_Padding(bKey, cEnData);
//        byte[] cByte03 = SM4Util.decrypt_ECB_NoPadding(bKey, cEnData);
//        byte[] cByte04 = SM4Util.decrypt_CBC_Padding(bKey, null,cEnData);
//        byte[] cByte05 = SM4Util.decrypt_CBC_NoPadding(bKey, null,cEnData);

//         gmKey = "ABCKG6vCSt1PZQX1/n7OLg==";
//         bKey = Base64.decodeBase64(gmKey);


        byte[] bytes = decrypt_Ecb_Padding(bKey, e1Data);
        byte[] bytes2 = decrypt_Ecb_Padding(bKey, e2Data);

        String s1 = new String(bytes, "UTF-8");
        String s2 = new String(bytes2, "UTF-8");


        System.out.println("还原：" + s1);
        System.out.println("还原：" + s2);



    }
}

